package com.distribute.portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.builder.StandardToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.pojo.DbUser;
import com.distribute.portal.pojo.Order;
import com.distribute.portal.service.OrderService;
/*import com.distribute.portal.service.OrderService;*/
import com.distribute.portal.service.UserService;

@Controller
@RequestMapping("/userhome")
public class UserHomeController {

	@Autowired
	UserService userService;
	@Autowired
	OrderService orderService;
	
	@RequestMapping("/user-home")
	public String showUserHome(HttpServletRequest request,Model model) {
		//拦截器拦截后会将user设置为request的属性，不必再去查询user信息
		//将user,orderList设置给model
		DbUser user = (DbUser) request.getAttribute("user");
		/*List<Order> list = orderService.getOrderByUserId(user.getId());*/
	/*	model.addAttribute("myOrderList",list);*/
		model.addAttribute("currentUser", user);
		return "user-home";
	}
	
	@RequestMapping(value="/checkPass",method=RequestMethod.POST)
	@ResponseBody
	public Map checkUserPass(String userName,String password) {
		Map<String , Boolean> map = new HashMap<>();
		try {
			DistributeResult result = userService.checkUserPass(userName, password);
			if(result.getStatus()==200) {
				map.put("valid",true);
				return map;
			}else {
				map.put("valid",false);
				return map;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			map.put("valid",false);
			e.printStackTrace();
			return map;
		}
	}
	@RequestMapping(value="/change/userPass",method=RequestMethod.POST)
	@ResponseBody
	public DistributeResult changeUserPass(String userName,String password,String token) {
		try {
			DistributeResult result = userService.updateUserPass(userName, password,token);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return DistributeResult.build(500, "error");
		}
	}
	@RequestMapping(value="/change/userInfo",method=RequestMethod.POST)
	@ResponseBody
	public DistributeResult changeUserInfo(String userName,String phone,String email,String token) {
		try {
			DistributeResult result = userService.updateUserInfo(userName, phone,email,token);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return DistributeResult.build(500, "error");
		}
	}
}
